import React, { Component } from 'react';
import CodeMock from './CodeMock';
import RenderArea from './RenderArea';

import './RenderMock.css';

import htmlMaker from './data/htmlMaker';
import cssMaker from './data/cssMaker';

let htmlCode = htmlMaker();
const cssCode = cssMaker();

export default class RenderMock extends Component {

  state = {
    defaultHtml: htmlCode,
    defaultCss: cssCode,
    html: htmlCode,
    css: cssCode,
    rendering: false
  }
  shouldComponentUpdate(nextProps, nextState) {
    return this.state.defaultHtml !== nextState.defaultHtml
      || this.state.defaultCss !== nextState.defaultCss
      || this.state.rendering !== nextState.rendering;
  }
  handleChange = (type, code) => {
    if(this.state.rendering) {
      return;
    }
    this.setState({
      [type]: code
    });
  }
  onBegin = () => {
    this.setState({
      rendering: true
    });
  }
  onEnd = () => {
    this.setState({
      rendering: false
    });
  }
  onChange = (defaultHtml) => {
    this.setState({ defaultHtml });
  }
  onSwitch = () => {
    this.setState({ defaultHtml: htmlCode });
  }
  render() {
    return (
      <div className="mock-container">
        <div className="clearfix code-area">
          <div className="code__box code__box--html">
            <CodeMock mode="htmlmixed" value={ this.state.defaultHtml } onChange={code => this.handleChange('html', code)} />
          </div>
          <div className="code__box code__box--css">
            <CodeMock mode="css" value={ this.state.defaultCss } onChange={code => this.handleChange('css', code)} />
          </div>
        </div>
        <RenderArea html={this.state.html} css={this.state.css} onBegin={this.onBegin} onChange={this.onChange} onEnd={this.onEnd} onSwitch={this.onSwitch}/>
      </div>
    )
  }
}
